﻿using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.ConstrainedExecution;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using TestingPlatForm.MissionContrl.Dal;
using TestingPlatForm.MissionContrl.Entity;

namespace TestingPlatForm.MissionContrl.ViewModels
{
    public class ConnectWayViewModel : BindableBase
    {
        #region

        private double _LinkWay;

        public double LinkWay
        {
            get { return _LinkWay; }
            set { SetProperty(ref _LinkWay , value); }
        }


        private double _upLinkPower;

        public double upLinkPower
        {
            get { return _upLinkPower; }
            set { SetProperty(ref _upLinkPower, value); }
        }

        private double _Lattenuation;

        public double Lattenuation
        {
            get { return _Lattenuation; }
            set { SetProperty(ref _Lattenuation, value); }
        }

        private double _receptionLoss;

        public double receptionLoss
        {
            get { return _receptionLoss; }
            set { SetProperty(ref _receptionLoss , value); }
        }

        private double _LXgain;

        public double LXgain
        {
            get { return _LXgain; }
            set { SetProperty(ref _LXgain, value); }
        }

        private double _XVattenuation;

        public double XVattenuation
        {
            get { return _XVattenuation; }
            set { SetProperty(ref _XVattenuation,value); }
        }

        private double _SendGain;

        public double SendGain
        {
            get { return _SendGain; }
            set { SetProperty(ref _SendGain, value); }
        }

        private double _receiveGain;

        public double receiveGain
        {
            get { return _receiveGain; }
            set { SetProperty(ref _receiveGain, value); }
        }

        private double _IFpower;

        public double IFpower
        {
            get { return _IFpower; }
            set { SetProperty(ref _IFpower, value); }
        }

        private double _downLinkWay;

        public double downLinkWay
        {
            get { return _downLinkWay; }
            set { SetProperty(ref _downLinkWay , value); }
        }


        private double _Xattenuation;

        public double Xattenuation
        {
            get { return _Xattenuation; }
            set { SetProperty(ref _Xattenuation , value); }
        }

        private double _gain;

        public double gain
        {
            get { return _gain; }
            set { SetProperty(ref _gain, value); }
        }

        private double _transmissionGain;

        public double transmissionGain
        {
            get { return _transmissionGain; }
            set { SetProperty(ref _transmissionGain , value); }
        }

        private double _receptionGain;

        public double receptionGain
        {
            get { return _receptionGain; }
            set { SetProperty(ref _receptionGain , value); }
        }

        private double _conversionLoss;

        public double conversionLoss
        {
            get { return _conversionLoss; }
            set { SetProperty(ref _conversionLoss ,value); }
        }

        private double _inputPower;

        public double inputPower
        {
            get { return _inputPower; }
            set { SetProperty(ref _inputPower, value);  }
        }


        private double _cableLoss;

        public double cableLoss
        {
            get { return _cableLoss; }
            set { SetProperty(ref _cableLoss, value); }
        }

        #endregion

        public ConnectWayViewModel()
        {
            LinkWay = GlobalEntity.LinkData.LinkWay;
            upLinkPower = GlobalEntity.LinkData.upLinkPower;
            Lattenuation = GlobalEntity.LinkData.Lattenuation;
            receptionLoss = GlobalEntity.LinkData.receptionLoss;
            LXgain = GlobalEntity.LinkData.LXgain;
            XVattenuation = GlobalEntity.LinkData.XVattenuation;
            SendGain = GlobalEntity.LinkData.SendGain;
            receiveGain = GlobalEntity.LinkData.receiveGain;
            IFpower = GlobalEntity.LinkData.IFpower;
            downLinkWay = GlobalEntity.LinkData.downLinkWay;
            Xattenuation = GlobalEntity.LinkData.Xattenuation;
            gain = GlobalEntity.LinkData.gain;
            transmissionGain = GlobalEntity.LinkData.transmissionGain;
            receptionGain = GlobalEntity.LinkData.receptionGain;
            conversionLoss = GlobalEntity.LinkData.conversionLoss;
            inputPower = GlobalEntity.LinkData.inputPower;
            cableLoss = GlobalEntity.LinkData.cableLoss;
        }

        public ICommand SaveCommand
        {
            get => new DelegateCommand(() =>
            {
                GlobalEntity.LinkData.LinkWay = LinkWay;
                GlobalEntity.LinkData.upLinkPower = upLinkPower;
                GlobalEntity.LinkData.Lattenuation = Lattenuation;
                GlobalEntity.LinkData.receptionLoss = receptionLoss;
                GlobalEntity.LinkData.LXgain = LXgain;
                GlobalEntity.LinkData.XVattenuation = XVattenuation;
                GlobalEntity.LinkData.SendGain = SendGain;
                GlobalEntity.LinkData.receiveGain = receiveGain;
                GlobalEntity.LinkData.IFpower = IFpower;
                GlobalEntity.LinkData.downLinkWay = downLinkWay;
                GlobalEntity.LinkData.Xattenuation = Xattenuation;
                GlobalEntity.LinkData.gain = gain;
                GlobalEntity.LinkData.transmissionGain = transmissionGain;
                GlobalEntity.LinkData.receptionGain = receptionGain;
                GlobalEntity.LinkData.conversionLoss = conversionLoss   ;
                GlobalEntity.LinkData.inputPower = inputPower;
                GlobalEntity.LinkData.cableLoss = cableLoss;
                

                string currentDirectory = Directory.GetCurrentDirectory();
                string logConfigFile = currentDirectory + "\\config\\LinkSetting.config";
                if (File.Exists(logConfigFile))
                {
                    string[] readText = File.ReadAllLines(logConfigFile);
                    for (int nIndex = 0; nIndex < readText.Length; nIndex++)
                    {
                        string line = readText[nIndex];
                        if (line.Contains("UPLinkWay="))
                        {
                            // 用户链路KU
                            readText[nIndex] = string.Format("UPLinkWay={0}", LinkWay);
                        }
                        if (line.Contains("upLinkPower="))
                        {
                            // 用户链路KU
                            readText[nIndex] = string.Format("upLinkPower={0}", upLinkPower);
                        }
                        if (line.Contains("Lattenuation="))
                        {
                            // 用户链路KU
                            readText[nIndex] = string.Format("Lattenuation={0}", Lattenuation);
                        }
                        if (line.Contains("receptionLoss="))
                        {
                            // 用户链路KU
                            readText[nIndex] = string.Format("receptionLoss={0}", receptionLoss);
                        }
                        if (line.Contains("LXGain="))
                        {
                            // 用户链路KU
                            readText[nIndex] = string.Format("LXGain={0}", LXgain);
                        }
                        if (line.Contains("XVattenuation="))
                        {
                            // 用户链路KU
                            readText[nIndex] = string.Format("XVattenuation={0}", XVattenuation);
                        }
                        if (line.Contains("SendGain="))
                        {
                            // 用户链路KU
                            readText[nIndex] = string.Format("SendGain={0}", SendGain);
                        }
                        if (line.Contains("receiveGain="))
                        {
                            // 用户链路KU
                            readText[nIndex] = string.Format("receiveGain={0}", receiveGain);
                        }
                        if (line.Contains("IFpower="))
                        {
                            // 用户链路KU
                            readText[nIndex] = string.Format("IFpower={0}", IFpower);
                        }
                        if (line.Contains("downLinkWay="))
                        {
                            // 用户链路KU
                            readText[nIndex] = string.Format("downLinkWay={0}", downLinkWay);
                        }
                        if (line.Contains("Xattenuation="))
                        {
                            // 馈电链路QV
                            readText[nIndex] = string.Format("Xattenuation={0}", Xattenuation);
                        }
                        if (line.Contains("gain="))
                        {
                            // 馈电链路QV
                            readText[nIndex] = string.Format("gain={0}", gain);
                        }
                        if (line.Contains("transmissionGain="))
                        {
                            // 馈电链路QV
                            readText[nIndex] = string.Format("transmissionGain={0}", transmissionGain);
                        }
                        if (line.Contains("receptionGain="))
                        {
                            // 馈电链路QV
                            readText[nIndex] = string.Format("receptionGain={0}", receptionGain);
                        }
                        if (line.Contains("conversionLoss="))
                        {
                            // 终端X
                            readText[nIndex] = string.Format("conversionLoss={0}", conversionLoss);
                        }
                        if (line.Contains("inputPower="))
                        {
                            // 基站X
                            readText[nIndex] = string.Format("inputPower={0}", inputPower);
                        }
                        if (line.Contains("cableLoss="))
                        {
                            // 基站X
                            readText[nIndex] = string.Format("cableLoss={0}", cableLoss);
                        }
                        
                    }

                    // 重写入配置文件
                    File.WriteAllLines(logConfigFile, readText);

                    MessageBox.Show("配置保存成功！");
                }
                InitialVal.RefreshValue();
            });
        }
    }
}
